package com.zhangdi.demo.text.chinesesort;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Created by zhangdi on 2020/06/09 12:00:26.
 */
public class ChineseEnUtils {
    private final static String GB2312 = "GB2312";
    private final static String ISO8859_1 = "ISO8859-1";
    private final static String OTHER_STR_FLAG = "#";

    private final static Collator collator = Collator.getInstance(Locale.CHINA);

    private final static int[] li_SecPosValue = {1601, 1637, 1833, 2078, 2274,
            2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858,
            4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590};

    private final static String[] lc_FirstLetter = {"A", "B", "C", "D", "E",
            "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
            "T", "W", "X", "Y", "Z"};

    //字典
    private static Map<String, String> exceptWords = new HashMap<>();

    static {
        exceptWords.put("A", "庵鳌");
        exceptWords.put("B", "璧亳並侼別匂蝙");
        exceptWords.put("C", "茌丞丒丳刅");
        exceptWords.put("D", "渎砀棣儋丟");
        exceptWords.put("E", "");
        exceptWords.put("F", "邡冹兝蝠");
        exceptWords.put("G", "崮藁莞丐丱乢亁仠冮匃匄");
        exceptWords.put("H", "骅珲潢湟丆冴匢");
        exceptWords.put("J", "泾蛟暨缙旌莒鄄丌丩丮丯丼亅伋冏匊匛匞");
        exceptWords.put("K", "丂匟");
        exceptWords.put("L", "崂涞栾溧漯浏耒醴泸阆崃両刢劽啰");
        exceptWords.put("M", "渑汨丏冐冺兞冇");
        exceptWords.put("N", "");
        exceptWords.put("O", "瓯");
        exceptWords.put("P", "邳濮郫丕伂冸");
        exceptWords.put("Q", "覃喬綦衢岐朐邛丠丬亝冾兛匤");
        exceptWords.put("R", "榕刄");
        exceptWords.put("S", "泗睢沭嵊歙莘嵩鄯丄丗侺兙");
        exceptWords.put("T", "潼滕郯亣侹侻");
        exceptWords.put("W", "婺涠汶亾仼卍卐");
        exceptWords.put("X", "胥鑫盱浔荥淅浠亵丅伈兇");
        exceptWords.put("Y", "闫懿眙黟颍兖郓偃鄢晏丣亜伇偐円匜");
        exceptWords.put("Z", "梓涿诏柘秭圳伀冑刣");
    }

    /**
     * 取得给定汉字的首字母,即声母
     *
     * @param chinese 给定的汉字
     * @return 给定汉字的声母
     */
    public static String getFirstLetter(String chinese) {
        if (chinese == null || chinese.trim().length() == 0) {
            return OTHER_STR_FLAG;
        }

        chinese = chinese.substring(0, 1);
        if (StringUtils.isNumeric(chinese)) {
            return OTHER_STR_FLAG;
        }

        chinese = conversionStr(chinese, GB2312, ISO8859_1);
        if (chinese.length() > 1) {
            int li_SectorCode = ((int) chinese.charAt(0)) - 160; // 区码
            int li_PositionCode = ((int) chinese.charAt(1)) - 160; // 位码
            int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // 区位码
            if (li_SecPosCode > 1600 && li_SecPosCode < 5590) {
                for (int i = 0; i < 23; i++) {
                    if (li_SecPosCode >= li_SecPosValue[i] && li_SecPosCode < li_SecPosValue[i + 1]) {
                        chinese = lc_FirstLetter[i];
                        break;
                    }
                }
            } else {
                chinese = matchPinYin(chinese);
            }
        } else {
            chinese = StringUtils.upperCase(chinese);
        }

        return chinese;
    }

    private static String matchPinYin(String chinese) {
        chinese = conversionStr(chinese, ISO8859_1, GB2312);
        for (Map.Entry<String, String> letterSet : exceptWords.entrySet()) {
            if (letterSet.getValue().contains(chinese)) {
                return letterSet.getKey();
            }
        }
        return OTHER_STR_FLAG;
    }

    /**
     * 字符串编码转换
     *
     * @param str           要转换编码的字符串
     * @param charsetName   原来的编码
     * @param toCharsetName 转换后的编码
     * @return 编码转换后的字符串
     */
    private static String conversionStr(String str, String charsetName,
                                        String toCharsetName) {
        try {
            str = new String(str.getBytes(charsetName), toCharsetName);
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        return str;
    }

    public static Comparator<String> LETTER_COMPARE = (s1, s2) -> {
        if (OTHER_STR_FLAG.equals(s1)) {
            return 1;
        } else if (OTHER_STR_FLAG.equals(s2)) {
            return -1;
        } else {
            return s1.compareTo(s2);
        }
    };

    public static Comparator<LetterSimple> OBJECT_COMPARE = (o1, o2) -> {
        if (OTHER_STR_FLAG.equals(o1.getFlag())) {
            return 1;
        } else if (OTHER_STR_FLAG.equals(o2.getFlag())) {
            return -1;
        } else {
            CollationKey k1 = collator.getCollationKey(o1.getFlag());
            CollationKey k2 = collator.getCollationKey(o2.getFlag());
            return k1.compareTo(k2);
        }
    };

    public static void main(String[] args) {
        System.out.println("获取拼音首字母：" + ChineseEnUtils.getFirstLetter("高吸"));
    }
}
